package cn.elwy.rcp.ui.table.viewpart;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import cn.elwy.rcp.core.model.ui.InputModel;
import cn.elwy.rcp.ui.table.TablePane;
import cn.elwy.rcp.ui.table.TablePaneFactory;

public abstract class AbstractSingleTableView extends ViewPart {

  protected TablePane tablePane;

  @Override
  public void createPartControl(Composite parent) {
    tablePane = TablePaneFactory.getTablePane(parent, getTablePaneId());
    parent.getDisplay().asyncExec(new Runnable() {
      @Override
      public void run() {
        tablePane.setInputModel(getInput());
      }

    });
    tablePane.setSelectionProvider(getSite());
    tablePane.registerContextMenu(getSite());
  }

  public TablePane getTablePane() {
    return tablePane;
  }

  protected abstract String getTablePaneId();

  protected abstract InputModel getInput();

  @Override
  public void setFocus() {
    if (tablePane != null) {
      tablePane.setFocus();
    }
  }

  @Override
  public void dispose() {
    if (tablePane != null) {
      tablePane.dispose();
    }
    super.dispose();
  }
}
